package com.example.mbl.service.impl;

import com.example.mbl.dao.BookMapper;
import com.example.mbl.entity.Book;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.BookPageResult;
import com.example.mbl.service.BookService;
import com.example.mbl.util.Tools;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> searchItemsByKeyWord(String keyword) {
        return bookMapper.searchItemsByKeyWord(keyword);
    }

    @Override
    public WebRestResult addBook(Book book) {
        WebRestResult result = new WebRestResult();
        book.setBookid(UUIDUtil.randomUUID());
        if(Tools.isEmpty(book.getFlag()))
            book.setFlag("0");
        book.setCreatedate(new Date());
        if(bookMapper.insert(book)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增书籍失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteBook(Book book) {
        WebRestResult result = new WebRestResult();
        if(bookMapper.deleteByPrimaryKey(book.getBookid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除书籍失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateBook(Book book) {
        WebRestResult result = new WebRestResult();
        if(bookMapper.updateByPrimaryKeySelective(book)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新书籍失败");
        }
        return result;
    }

    @Override
    public WebRestResult setBookVisible(Book book) {
        WebRestResult result = new WebRestResult();
        if(bookMapper.setBookVisible(book.getBookid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("设置失败");
        }
        return result;
    }

    @Override
    public PageResult<BookPageResult> getBookList(String bookname, String publishdate, int page, int pagesize) {
        PageResult<BookPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<BookPageResult> list = bookMapper.getBookList(bookname,publishdate,start,pagesize);
        if(list.size()>0){
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }
}
